<?php
class Shop_model extends CI_Model {
    
   
    private $_tabla = 'shop';
    private $_tabla_2 = 'shop_details';
    
    public function add_shop($data){
       $this->db->insert($this->_tabla, $data);
	   return $this->db->insert_id();
    }
    
    public function add_shop_dateils($data){
       return $this->db->insert($this->_tabla_2, $data);
    }
    
	public function getAll(){
		$query = $this->db->query("SELECT * FROM Shop");
        return $query->result(); 
	}
	
    public function search_shop_deatils($document_code){ 
        $query = $this->db->query("SELECT p.code,p.description, sd.box, sd.pack, sd.unit, sd.Val_Unit, sd.amount FROM products as p, shop_details as sd where p.products_id = sd.product_id and sd.document_code = '".$document_code."'");
        $rs = $query->result();
        return $rs;                      
    }
    
	public function deleteAllDetails($shop_id){
		$this->db->query("DELETE FROM shop_details WHERE shop_id='".$shop_id."'");
	}
	
    public function delete_detail($document_code, $code){
       return $this->db->query("DELETE FROM shop_details WHERE  document_code='".$document_code."' AND product_code ='".$code."'");
    }
    
    public function delete_detail_2($document_code, $code){
       return $this->db->query("DELETE FROM shop_details WHERE  document_code='".$document_code."'");
    }
    
    public function update_shop($shop_id,$data)
    {        
        $this->db->where('id_shop', $shop_id);
        $this->db->update('shop', $data); 
    }
	
	public function findById($shop_id){
	    $this->db->select();
        $this->db->where('id_shop', $shop_id);
        $query = $this->db->get($this->_tabla);
        $rs = $query->result();
        
		if(count($rs)>0)
		    return $rs[0];
		else
			return false;
	}
	
	public function findDetails($shop_id){
		$query = $this->db->query("SELECT p.products_id, p.code, p.description, sd.box, sd.pack, sd.unit, sd.Val_Unit, sd.amount FROM products as p, shop_details as sd where p.products_id = sd.product_id and sd.shop_id = '".$shop_id."'");
        $rs = $query->result();
        return $rs;   
	}
	
	public function search($code = null)
    {
        $query = $this->db;
        if (isset($code) && $code != '') {
            $query = $query->like($this->_tabla.'.document_code', $code, 'both');
        }
       
        $query = $query->get($this->_tabla);
        $rs = $query->result();
        return $rs;
     }
    
    public function search_shop($document_code){
        $this->db->select();
        $this->db->where('document_code', ''.$document_code.'');
        $query = $this->db->get($this->_tabla);
        $rs = $query->result();
        
		if(count($rs)>0)
		    return $rs[0];
		else
			return false;
    }
    
    public function edit_amount($document_code,$amount){
         return $this->db->query("UPDATE shop SET amount='".$amount."' WHERE `document_code`='".$document_code."'");
    }
    
    public function comparaction_code($document_code){
        $query = $this->db->query("SELECT document_code FROM shop where document_code = '".$document_code."'");
        $rs = $query->num_rows();
        return $rs;
    }
    
     public function select_almacen_productos($code_product, $code_almacen){
       $query = $this->db->query("SELECT * FROM `warehouses_product` WHERE `id_product` = '".$code_product."' and `id_warehouse` = '".$code_almacen."'");
       $rs = $query->result();
       return $rs;
    }
    
    
    
    
    
}